<?php
    ob_start();
    session_name('dyh_session');
    session_start(); //start a session
    if (!(isset($_SESSION['fname']) && isset($_SESSION['email']) && isset($_COOKIE['dyh_region']))){
        /*remove PHPSESSID cookie from browser, that was created by session_start() above.*/
        setcookie( session_name(), '', time()-3600, '/');
        setcookie('dyh_region', '', time()-3600, '/');
        //user not registered so redirect to register page
        header('location:page2.html?lan=en');
        die();
    } else {
        //echo "Your payment was successfull. Thank you!! Redirecting you to Drawing page now";
        //db settings page
        require_once("db-settings.php"); //Require DB connection

        //get data from session object and insert in DB
        $fname = $_SESSION['fname'];
        $lname = $_SESSION['lname'];
        $city = $_SESSION['city'];
        $country = $_SESSION['country'];
        $email = $_SESSION['email'];
        $age = $_SESSION['age'];
        $phone = $_SESSION['phone'];

        $payment = "5.55";

        //save payment status in session
        $_SESSION['payment'] = 'done';

        //validate email for returning users
        $stmt = $mysqli -> prepare("SELECT
                firstname,lastname,city,country,email FROM users
                WHERE email = ?
                ");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $stmt->store_result();
        $num_returns = $stmt->num_rows;

        $stmt->bind_result($fn,$ln,$cty,$cntry,$e);
        if ($num_returns > 0) //RETURNING USER
        {
            //echo "Old User";
            //email exists - fetch user details
            while ($stmt->fetch()){
                $row = array('fname' => $fn, 'lname' => $ln, 'city' => $cty, 'country' => $cntry, 'email' => $e);
            }
            $stmt->close(); //SELECT
            $_SESSION['fname'] = $row['fname']; //get value from db since he is a returning user
            header('location:page6.php');
            die();
        } else {               //NEW USER
            //email does not exist previously- insert data into db
            $stmt = $mysqli->prepare("INSERT INTO users (
                firstname,
                lastname,
                city,
                country,
                email,
                age,
                phone,
                timestamp,
                payment
                )
                VALUES (
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?
                )");
            $stmt->bind_param("sssssssis", $fname,$lname,$city,$country,$email,$age,$phone,mktime(),$payment);
            $result = $stmt->execute();
            $stmt->close();  //INSERT
            if($result) {
                //echo "Data entered for New user";
                $row = array('fname' => $fname, 'lname' => $lname, 'city' => $city, 'country' => $country, 'email' => $email);
                header('location:page6.php');
                die();
            } else {
                echo "SQL error in inserting data";
            }
        }
        //based on region selected redirect user to drawing page
        /*if($_COOKIE['dyh_region'] == 'Can1') {
            header('location:page6.php');
        } else {
            header('location:page6.php');
        }*/
        //echo '<br/>Welcome : ' . $_SESSION['fname'] . ' Payment: ' . $_SESSION['payment'];
    }
    ob_end_flush();
?>